import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';

class OutDetailPage extends StatefulWidget {
  final Map arguments;
  OutDetailPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<OutDetailPage> {
  Map detail;
  bool loadSuccess;
  String baseApi = '';

  @override
  void initState() {
    super.initState();
    String model = widget.arguments['item']['actionModel'];

    if (model == 'scrap') {
      setState(() {
        baseApi = 'scrapGoods/';
      });
    } else if (model == 'allocate') {
      setState(() {
        baseApi = 'allocateGoods/';
      });
    } else {
      setState(() {
        baseApi = 'goods/';
      });
    }

    _loadData();
  }

  // 加载数据
  void _loadData() async {
    var id = widget.arguments['item']['goods_id'];
    loadSuccess = false;

    DioUtils.request("${baseApi}details",
        context: context,
        method: "POST",
        params: {
          'id': id,
        }, onSuccess: (res) {
      if (res['code'] == 1) {
        if (mounted) {
          setState(() {
            detail = res["data"];
            loadSuccess = true;
          });
        }
      } else {
        Utils.showText(text: res['msg']);
      }
    }, onError: (error) {});
  }

  Widget _buildCardHead(title) {
    return Container(
      width: 680.w,
      height: 90.w,
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
          border:
              Border(bottom: BorderSide(width: 1, color: Color(0xFFF1F1F1)))),
      child: Text(
        title,
        style: TextStyle(
            color: Color(0xFF333333),
            fontSize: 16,
            fontWeight: FontWeight.bold),
      ),
    );
  }

  Widget _buildRowText(title, text, [Color color]) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(title.toString(),
              style: TextStyle(color: Color(0xFF999999), fontSize: 14)),
          Container(
            width: 500.w,
            alignment: Alignment.centerRight,
            child: Text(text.toString(),
                maxLines: 1,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  color: color ?? Color(0xFF333333),
                  fontSize: 14,
                )),
          )
        ],
      ),
    );
  }

  Widget _buildRowPicture() {
    List<Widget> images = [];
    List<String> imgList = [];

    for (var i = 0; i < detail['enter_files'].length; i++) {
      var item = detail['enter_files'][i];

      imgList.add(DioUtils.API_PREFIX + item['file_path']);

      images.add(
        GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, "/photo_view",
                  arguments: {'list': imgList, 'index': i});
            },
            child: Container(
                width: 100.w,
                height: 100.w,
                decoration: BoxDecoration(
                  color: Utils.hexToColor('#F1F3F4'),
                  borderRadius: BorderRadius.circular(4.w),
                ),
                margin: EdgeInsets.only(right: 10.w),
                child: ClipRRect(
                    borderRadius: BorderRadius.circular(4.w),
                    child: Image.network(
                      DioUtils.API_PREFIX + item['file_path'],
                      fit: BoxFit.cover,
                    )))),
      );

      // images.add(
      //   Container(
      //     width: 100.w,
      //     height: 100.w,
      //     decoration: BoxDecoration(
      //       color: Utils.hexToColor('#F1F3F4'),
      //       borderRadius: BorderRadius.circular(4.w),
      //     ),
      //     margin: EdgeInsets.only(right: 10.w),
      //     child: ClipRRect(
      //       borderRadius: BorderRadius.circular(4.w),
      //       child: Image.network(DioUtils.API_PREFIX+item['file_path'],
      //         fit: BoxFit.cover,
      //       )
      //     )
      //   )
      // );
    }

    return Padding(
        padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
        child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Text('图片',
                  style: TextStyle(color: Color(0xFF999999), fontSize: 14)),
              Container(
                  width: 500.w,
                  child: Wrap(
                      spacing: 10.w,
                      runSpacing: 25.w,
                      alignment: WrapAlignment.end,
                      children: images))
            ]));
  }

  // Widget _buildRowPicture() {
  //   return Padding(
  //     padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
  //     child: Row(
  //       mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //       children: <Widget>[
  //         Text('图片', style: TextStyle(
  //           color: Color(0xFF999999),
  //           fontSize: 14
  //         )),
  //         Row(
  //           children: <Widget>[
  //             Container(
  //               margin: EdgeInsets.only(right: 10.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png',
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             ),
  //             Container(
  //               margin: EdgeInsets.only(left: 5.w),
  //               child: ClipRRect(
  //                 borderRadius: BorderRadius.circular(4.w),
  //                 child: Image.asset('assets/pro.png',
  //                   width: 100.w,
  //                   height: 100.w,
  //                 ),
  //               ),
  //             )
  //           ],
  //         )
  //       ],
  //     ),
  //   );
  // }

  Widget _buildPageOne() {
    if (loadSuccess) {
      return SingleChildScrollView(
          padding: EdgeInsets.fromLTRB(25.w, 0.w, 25.w, 60.w),
          child: Container(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 700.w,
                  margin: EdgeInsets.only(top: 24.w),
                  padding: EdgeInsets.only(bottom: 34.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(20.w),
                  ),
                  child: Column(
                    children: <Widget>[
                      _buildCardHead('基本信息'),
                      Container(
                        padding: EdgeInsets.fromLTRB(24.w, 20.w, 24.w, 0),
                        child: Column(
                          children: <Widget>[
                            _buildRowText('物资编号', detail['code']),
                            _buildRowText('物资名称', detail['goods_name']),
                            _buildRowText('规格/型号', detail['goods_model']),
                            _buildRowText(
                                '物资分类',
                                detail['type_name'] != null
                                    ? detail['type_name']
                                    : '--'),
                            _buildRowText('出库类型', '--'),
                            _buildRowText('计量单位', detail['goods_unit']),
                            _buildRowText(
                                '产品估价',
                                detail['goods_price'] != null
                                    ? detail['goods_price']
                                    : '--'),
                            _buildRowText('有效期', detail['valid_time']),
                            _buildRowText('来源单位', detail['goods_source']),
                            _buildRowText('所属仓库', detail['store_name']),
                            _buildRowText('所属库区', detail['store_library_name']),
                            _buildRowText(
                                '所属库位',
                                detail['store_detail_code'] != null
                                    ? detail['store_detail_code']
                                    : '--'),
                            _buildRowText('出库人', '--'),
                            _buildRowText('出库时间', detail['create_time']),
                            _buildRowText('RFID编码', detail['rfid_code']),
                            _buildRowText('出库工单', '--'),
                            _buildRowPicture(),
                            _buildRowText('备注', detail['remark']),
                          ],
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ));
    } else {
      return Container();
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return DefaultTabController(
        length: 3,
        initialIndex: 0,
        child: Scaffold(
            appBar: TopBar(
              title: '物资详情',
              backgroundColor: Utils.hexToColor('#297CFF'),
            ),
            body: _buildPageOne()));
  }
}
